package com.szh.mysobproject.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.szh.mysobproject.R;
import com.szh.mysobproject.model.VideoInfo;
import com.szh.mysobproject.widget.SampleCoverVideo;

import java.util.List;

public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.VideoHolder> {

    public static final String TAG = "VideoAdapter";

    private List<VideoInfo.ItemListBean> mDatas;

    public VideoAdapter(List<VideoInfo.ItemListBean> mDatas) {
        this.mDatas = mDatas;
    }

    @NonNull
    @Override
    public VideoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_videoplay, parent, false);
        return new VideoHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull VideoHolder holder, int position) {
        VideoInfo.ItemListBean.DataBean data = mDatas.get(position).getData();
        holder.gsyVideoPlayer.setUpLazy(data.getPlayUrl(), true, null, null, data.getTitle());
        //增加title
        holder.gsyVideoPlayer.getTitleTextView().setVisibility(View.GONE);
        //设置返回键
        holder.gsyVideoPlayer.getBackButton().setVisibility(View.GONE);
        //设置全屏按键功能
        holder.gsyVideoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.gsyVideoPlayer.startWindowFullscreen(holder.context, false, true);
            }
        });

        holder.gsyVideoPlayer.loadCoverImage(data.getPlayUrl(),R.drawable.image_error_bg);

        //防止错位设置
        holder.gsyVideoPlayer.setPlayTag(TAG);
        holder.gsyVideoPlayer.setPlayPosition(position);
        //是否根据视频尺寸，自动选择竖屏全屏或者横屏全屏
        holder.gsyVideoPlayer.setAutoFullWithSize(true);
        //音频焦点冲突时是否释放
        holder.gsyVideoPlayer.setReleaseWhenLossAudio(false);
        //全屏动画
        holder.gsyVideoPlayer.setShowFullAnimation(true);
        //小屏时不触摸滑动
        holder.gsyVideoPlayer.setIsTouchWiget(false);

    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class VideoHolder extends RecyclerView.ViewHolder {
        private TextView tvAuthor;
        private SampleCoverVideo gsyVideoPlayer;
        private Context context;

        public VideoHolder(@NonNull View itemView) {
            super(itemView);
            context = itemView.getContext();
            gsyVideoPlayer = itemView.findViewById(R.id.item_video_gsy);
            tvAuthor = itemView.findViewById(R.id.item_video_tv_author);
        }
    }
}
